//
//  LKDeseaseTypeTBC.m
//  QuickCure
//
//  Created by 胡不归是大虾 on 9/12/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "LKDeseaseTypeTBC.h"
#import "LKDeseaseTypeCell.h"
#import "LKdeseaseDescCell.h"
#import "LKDeseaseIMGCell.h"
#import "LKDesTypeTBC.h"
#import "LKBasTBC.h"
#import "LKCaseModel.h"

@interface LKDeseaseTypeTBC () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**
 *  疾病类型字符串
 */
@property(nonatomic,copy)NSString* typeCellStr;

/**
 *  疾病细分字符串
 */
@property(nonatomic,copy)NSString* basicTypeCellStr;

/**
 *  病例图片
 */
@property(nonatomic,strong)UIImage* caseImage;

@end

@implementation LKDeseaseTypeTBC
#pragma mark--设置tableView组样式
-(instancetype)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
    }
    return self;
}
#pragma mark--生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpMyUI];
}
#pragma mark--更新UI
-(void)setUpMyUI
{
    //修改导航栏
    self.navigationItem.title = @"增加病例";
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //创建提交按钮
    UIBarButtonItem* commitBtnItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(didClickCommitBtnItem)];
    self.navigationItem.rightBarButtonItem = commitBtnItem;
    //监听通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveTextViewChangeNoti:) name:UITextViewTextDidChangeNotification object:nil];
}


#pragma mark--通知执行的方法
-(void)didReceiveTextViewChangeNoti:(NSNotification*)notification
{
    //强转
    UITextView* textView = notification.object;
    //赋值
    self.model.illnessDescStr = textView.text;
}
#pragma mark - tableView数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果组数为1
    if (indexPath.section == 0) {
        LKDeseaseTypeCell* cell = [[LKDeseaseTypeCell alloc] init];
        cell.tag = indexPath.row;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //如果疾病类型有值
        if (self.typeCellStr.length != 0) {
            cell.desType = self.typeCellStr;
        }
        //如果疾病细分有值
        if (self.basicTypeCellStr.length != 0 ) {
            cell.basType = self.basicTypeCellStr;
        }
        return cell;
    }
    else//如果组数不为0 ,则为1
    {
        if (indexPath.row == 0) {
            LKdeseaseDescCell* cell = [[LKdeseaseDescCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }else
        {
            LKDeseaseIMGCell* cell = [[LKDeseaseIMGCell alloc] init];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.addImageBlock = ^{
                //    //创建图片选择控制器
                UIImagePickerController* imagePickController = [[UIImagePickerController alloc] init];
                //设置源
                imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                //弹出控制器
                [self presentViewController:imagePickController animated:YES completion:nil];
                //设置代理
                imagePickController.delegate = self;
            };
            if (self.caseImage) {
                cell.caseImage = self.caseImage;
            }
            return cell;
        }
    }
}
#pragma mark--tableView代理方法
//返回组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)//判断是哪一组然后返回不同的行高
    {
        return 150;
    }else
    {
        return 50;
    }
    
}
//选中cell跳转
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            LKDesTypeTBC* desTBC = [[LKDesTypeTBC alloc] init];
            desTBC.desTypeBlock = ^(NSString* descType)
            {
                self.typeCellStr = descType;
            };
            [self.navigationController pushViewController:desTBC animated:YES];
        }
        else if(indexPath.row == 1)
        {
            //            如果疾病类型还没挑选,就不能push
            if (self.typeCellStr.length == 0) {
                [SVProgressHUD setMinimumDismissTimeInterval:2];
                [SVProgressHUD showInfoWithStatus:@"请先选择疾病类型!"];
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            }
            else{
                LKBasTBC* basTBC = [[LKBasTBC alloc] init];
                basTBC.illnessTypeString = self.typeCellStr;
                basTBC.basicTypeBlock = ^(NSString* basType)
                {
                    self.basicTypeCellStr = basType;
                };
                [self.navigationController pushViewController:basTBC animated:YES];
            }
        }
    }
}
#pragma mark--imagePickerController的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //获取选中的图片
    UIImage* selectedImage = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    //将图片传值给cell
    self.caseImage  = selectedImage;
    //返回界面
    [picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark--按钮触发事件
-(void)didClickCommitBtnItem
{
    if(self.model.illnessTypeStr.length == 0 || self.model.basicTypeStr.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"请选择具体疾病细分!"];
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        [SVProgressHUD setMinimumDismissTimeInterval:1.5];
        return;
    }
    //获取当前时间
    NSDate* currentDate = [NSDate date];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    self.model.commitTime = [dateFormatter stringFromDate:currentDate];
    //传值给主控制器
    if (_modelBlock) {
        _modelBlock(self.model);
    }
    //推出控制器
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark--重写setter方法
//疾病类型传值
-(void)setTypeCellStr:(NSString *)typeCellStr
{
    _typeCellStr = typeCellStr;
    self.model.illnessTypeStr = typeCellStr;
    [self.tableView reloadData];
}
//基本类型传值
-(void)setBasicTypeCellStr:(NSString *)basicTypeCellStr
{
    _basicTypeCellStr = basicTypeCellStr;
    self.model.basicTypeStr = basicTypeCellStr;
    [self.tableView reloadData];
}

-(void)setCaseImage:(UIImage *)caseImage
{
    _caseImage = caseImage;
    self.model.caseImage = caseImage;
    [self.tableView reloadData];
}
#pragma mark--控制器销毁移除观察者
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark--数据懒加载
-(LKCaseModel *)model
{
    if (!_model) {
        _model = [[LKCaseModel alloc] init];
    }
    return _model;
}


@end
